package hibernate.dao;

import hibernate.entity.Agendamento;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

public class AgendamentoDAO {
	
	
private Session session;
	
	public AgendamentoDAO(){
		
	}
	
	public void adiciona(Agendamento agendamento){		
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.save(agendamento);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
	}	

	public void altera(Agendamento agendamento){		
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.update(agendamento);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}
	}	

	public void remove(Agendamento agendamento){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();		
		Transaction transaction = session.beginTransaction();
				
    	try{
			session.delete(agendamento);
			transaction.commit();
		} catch(Exception e){
			if(transaction != null)
				transaction.rollback();
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}	
	}	
	
	public Agendamento importaDados(String nome){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Agendamento.class);
		Agendamento agendamento = null;
		
    	try{
    		criteria.add(Restrictions.eq("nome", nome));
    		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    		agendamento = (Agendamento) criteria.uniqueResult();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		
		return agendamento;
	}
	
	public List<Agendamento> getLista(){
		
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Agendamento.class);		
		List<Agendamento> listaAgendamento = null;
		
    	try{
			listaAgendamento = (List<Agendamento>) criteria.list();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		return listaAgendamento;
	}
	
	
	public List<Agendamento> getAgendamento(String data){
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Agendamento.class);		
		List<Agendamento> listaAgendamento = null;
		
    	try{
    		criteria.setFetchMode("servico", FetchMode.JOIN);
    		criteria.setFetchMode("cliente", FetchMode.JOIN);
    		criteria.setFetchMode("funcionario", FetchMode.JOIN);
    		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    		criteria.add(Restrictions.eq("data",new SimpleDateFormat("dd/MM/yyyy").parse(data)));
			listaAgendamento = (List<Agendamento>) criteria.list();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		return listaAgendamento;
	}
	
	public List<Agendamento> searchAgendamento(String data,long cliente,long funcionario){
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Agendamento.class);		
		List<Agendamento> listaAgendamento = null;
		
    	try{
    		criteria.setFetchMode("servico", FetchMode.JOIN);
    		criteria.setFetchMode("cliente", FetchMode.JOIN);
    		criteria.setFetchMode("funcionario", FetchMode.JOIN);
    		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    		criteria.add(Restrictions.disjunction()
    				.add(Restrictions.eq("data",new SimpleDateFormat("dd/MM/yyyy").parse(data)))
    				.add(Restrictions.eq("cliente.id", cliente))
    				.add(Restrictions.eq("funcionario.id", funcionario))
    		);
			listaAgendamento = (List<Agendamento>) criteria.list();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		return listaAgendamento;
	}
	
	
	public List<Agendamento> getHistorico(Date dataIni,Date dataFim,long cliente){
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Agendamento.class);		
		List<Agendamento> listaAgendamento = null;
		
    	try{
    		criteria.setFetchMode("servico", FetchMode.JOIN);
    		criteria.setFetchMode("cliente", FetchMode.JOIN);
    		criteria.setFetchMode("funcionario", FetchMode.JOIN);
    		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    		criteria.add(Restrictions.between("data", dataIni, dataFim));
    		criteria.add(Restrictions.eq("cliente.id", cliente));
    		
			listaAgendamento = (List<Agendamento>) criteria.list();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		return listaAgendamento;
	}
	
	
	public List<Agendamento> getMovimentacao(Date dataIni,Date dataFim){
		HibernateConnection.buildSessionFactory();
		session = HibernateConnection.openSession();			
		Criteria criteria = session.createCriteria(Agendamento.class);		
		List<Agendamento> listaAgendamento = null;
		
    	try{
    		criteria.setFetchMode("servico", FetchMode.JOIN);
    		criteria.setFetchMode("cliente", FetchMode.JOIN);
    		criteria.setFetchMode("funcionario", FetchMode.JOIN);
    		criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    		criteria.add(Restrictions.between("data", dataIni, dataFim));
    		
    		
			listaAgendamento = (List<Agendamento>) criteria.list();
		} catch(Exception e){
		    System.out.println("Erro: " + e);
		} finally {
			HibernateConnection.closeSessionFactory();
			session.clear();
			session.close();
		}			
		return listaAgendamento;
	}
	
	
	
}
